package cn.kgm.queue;

import java.util.concurrent.ArrayBlockingQueue;

public class SharedQueueV1 {
    private static Object add = new Object();
    private static Object consumer = new Object();
    private static int queueMaxSize = 16;
    private static final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(queueMaxSize);
    private static int queueSize = 0;
    private static boolean queuelive = false;


    class Consumer extends Thread {
        @Override
        public void run() {
            while (true) {
                synchronized (queue) {
                    if (queue.isEmpty()) {
                        // wait()可以用在任何对象上,但是 这个对象必须成为锁对象
                        try {
                            // queue为空的时候停止while的执行,这样可以不消耗内存
                            queue.wait();
                        } catch (InterruptedException e) {
                            // 出现异常必须唤醒线程,否则这个线程将永远沉睡下去
                            queue.notify();
                        }
                    }
                }
            }
        }
    }
}
